﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DoAnTriTueNhanTao
{
    class Sprite
    {
        private Bitmap[] _bmp;

        public Bitmap[] bmp
        {
            get { return _bmp; }
            set 
            {
                _bmp = value;
                nBitmap = _bmp.Length;
            }
        }
        private int _nBitmap;

        public int nBitmap
        {
            get { return _nBitmap; }
            set { _nBitmap = value; }
        }
        private int _Top;

        public int Top
        {
            get { return _Top; }
            set { _Top = value; }
        }
        private int _Left;

        public int Left
        {
            get { return _Left; }
            set { _Left = value; }
        }
        private int _Width;

        public int Width
        {
            get { return _Width; }
            set { _Width = value; }
        }
        private int _Height;

        public int Height
        {
            get { return _Height; }
            set { _Height = value; }
        }
        private int _index;

        public int index
        {
            get { return _index; }
            set { _index = value; }
        }
        private int[] _seq;

        public int[] seq
        {
            get { return _seq; }
            set { _seq = value; }
        }
        private int _iSeq;

        public int iSeq
        {
            get { return _iSeq; }
            set { _iSeq = value; }
        }
        private bool _visible;

        public bool visible
        {
            get { return _visible; }
            set { _visible = value; }
        }

        private bool _isHover;

        public bool isHover
        {
            get { return _isHover; }
            set { _isHover = value; }
        }

        public Sprite(Image img,int width,int height)
        {
            Left = 0;
            Top = 0;
            Width = width;
            Height = height;
            visible = true;
            isHover = false;
            //------------------------QNR
            int[] sq = { 0 };
            seq = sq;
            iSeq = 0;
            index = seq[iSeq];
            //------------------------
            Bitmap temp = new Bitmap(img);
            bmp = splitBitmap(temp, width, height);
            for (int i = 0; i < nBitmap; i++)
            {
                //bmp[i].MakeTransparent(bmp[i].GetPixel(0, 0));
            }

        }
        public Sprite(Image img)
        {
            Left = 0;
            Top = 0;
            Width = img.Width;
            Height = img.Height;
            visible = true;
            isHover = false;
            bmp = new Bitmap[1];
            bmp[0] = new Bitmap(img);
            //bmp[0].MakeTransparent(bmp[0].GetPixel(0, 0));
        }
        public void Draw(Graphics g)
        {
            if (visible&&!isHover)
            {
                g.DrawImage(bmp[index], Left, Top);
            }
            else if (visible && isHover)
            {
                g.DrawImage(bmp[index], Left - 2, Top - 2, Width + 4, Height + 4);
            }
        }
        public void setPosition(int x, int y)
        {
            Left = x - Width / 2;
            Top = y - Height / 2;
        }
        public int getX()
        {
            return Left + Width / 2;
        }
        public int getY()
        {
            return Top + Height / 2;
        }
        public void Move(int x, int y)
        {
            Left += x;
            Top += y;
        }
        public void SetFrame(int idx)
        {
            index = idx;
        }
        public int getWidth()
        {
            return Width;
        }
        public int getHeight()
        {
            return Height;
        }
        public void setFrameSequence(int[] s)
        {
            seq = s;
            index = seq[0];
            iSeq = 0;
        }
        public void NextFrame()
        {
            iSeq = (iSeq + 1) % seq.Length;
            index = seq[iSeq];
        }
        public void setVisible(bool vis)
        {
            visible = vis;
        }
        public bool CollidesWith(Sprite sprite)
        {
            if (visible && sprite.visible)
            {
                int redundantW1=Width*20/100;
                int redundantH1=Height*20/100;
                int redundantW2=sprite.Width*20/100;
                int redundantH2=sprite.Height*20/100;
                int top1 = Top +redundantH1;
                int bottom1 = Top + Height - redundantH1;
                int left1 = Left + redundantW1;
                int right1 = Left + Width - redundantW1;
                int top2 = sprite.Top + redundantH2;
                int bottom2 = sprite.Top + sprite.Height - redundantH2;
                int left2 = sprite.Left + redundantW2;
                int right2 = sprite.Left + sprite.Width - redundantW2; 
                if (bottom1 >= top2 && top1 <= bottom2 && left1 <= right2 && right1 >= left2)
                    return true;
            }
            return false;
        }
        public void SetImage(Image img, int width, int height)
        {
            Bitmap temp = new Bitmap(img);
            bmp = splitBitmap(temp, width, height);
            for (int i = 0; i < nBitmap; i++)
            {
                bmp[i].MakeTransparent(bmp[i].GetPixel(0, 0));
            }
        }
        public void RotateSprite(RotateFlipType type)
        {
            for (int i = 0; i < nBitmap; i++)
            {
                bmp[i].RotateFlip(type);
            }
        }
        public bool isClicked(int x, int y)
        {
            if (Left <= x && Left + Width >= x && Top <= y && Top + Height >= y && visible)
            {
                return true;
            }
            return false;
        }
        public bool IsHover(int x, int y)
        {
            if (Left <= x && Left + Width >= x && Top <= y && Top + Height >= y && visible)
            {
                isHover = true;
                return true;
            }
            else
                isHover = false;
            return false;
        }

        private Bitmap[] splitBitmap(Bitmap bmp, int w, int h)
        {
            Bitmap[] kq;
            int nW = (bmp.Width) / w;
            int nH = (bmp.Height) / h;
            int idx = 0;
            kq = new Bitmap[nW * nH];
            Graphics g;
            for (int i = 0; i < nH; i++)
            {
                for (int j = 0; j < nW; j++)
                {
                    kq[idx] = new Bitmap(w, h);
                    g = Graphics.FromImage(kq[idx]);
                    g.DrawImage(bmp, -w * j, -h * i);
                    g.Dispose();
                    idx++;
                }
            }
            return kq;
        }
    }
}
